<html>
<!--
 -  
 -  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 -  project.
 -  
 -  Copyright (C) 1998-2025 OpenLink Software
 -  
 -  This project is free software; you can redistribute it and/or modify it
 -  under the terms of the GNU General Public License as published by the
 -  Free Software Foundation; only version 2 of the License, dated June 1991.
 -  
 -  This program is distributed in the hope that it will be useful, but
 -  WITHOUT ANY WARRANTY; without even the implied warranty of
 -  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 -  General Public License for more details.
 -  
 -  You should have received a copy of the GNU General Public License along
 -  with this program; if not, write to the Free Software Foundation, Inc.,
 -  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 -  
 -  
-->
<HEAD>
  <script language="JavaScript" src="../ajax_inc/ajax.js"></script>
  <script language="JavaScript" src="../ajax_inc/proxy.vsp?wsdl_path=exchange/services.wsdl"></script>

  <link rel="stylesheet" type="text/css" href="/tutorial/services/demo.css" />
<SCRIPT language="JavaScript">
var currencyFrom = 'USD';
var currencyTo = 'USD';
var amount = '1';
var browsertype;

if (document.implementation.createDocument)
{ 
    browsertype='Mozila';
   // Mozilla, create a new DOMParser 
   var parser = new DOMParser();
   var xsltProcessor = new XSLTProcessor(); 


    // load the xslt file
    var myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", "currency_list_ajax.xsl", false);
    myXMLHTTPRequest.send(null);
    
    // parse XSLT text to XML DOM(when not local responseXML is set null ???)
    listToXSL = parser.parseFromString(myXMLHTTPRequest.responseText, "text/xml");

    var myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", "converted_value_ajax.xsl", false);
    myXMLHTTPRequest.send(null);
    
    // parse XSLT text to XML DOM(when not local responseXML is set null ???)
    amountXSL = parser.parseFromString(myXMLHTTPRequest.responseText, "text/xml");


}
else if (window.ActiveXObject)
{ 
  browsertype='IE';
  var listToXSL = new ActiveXObject("Microsoft.XMLDOM");
  var amountXSL = new ActiveXObject("Microsoft.XMLDOM");

  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");


  listToXSL.async = false;
  amountXSL.async = false;
  xmlDoc.async = false;

  listToXSL.load("currency_list_ajax.xsl");
  amountXSL.load("converted_value_ajax.xsl");

}


function convertRateAjax()
{
  amount = document.getElementById('amount').value;
  currencyFrom = document.getElementById('currencyFrom').value;
  currencyTo = document.getElementById('currencyTo').value;
  if (amount != '' && currencyFrom != '' && currencyTo != '')
  {

    ajax.Start(convertRate,'');
  }
  else if (amount == '')
    alert('Please enter an amount.');
  else if (currencyFrom == '' || currencyTo == '')
    alert('Please choose currencies.');
}

function convertRatePrepare()
{
  return Array(document.getElementById('currencyFrom').value, document.getElementById('currencyTo').value, document.getElementById('amount').value);
}


function amResultsAjax(result)
{
     try
    	{
             if(browsertype=='IE')
             {
               xmlDoc.loadXML (result);
               document.getElementById('divConvertedAmount').innerHTML = xmlDoc.transformNode(amountXSL.documentElement);
             }
             else
             {  
               xmlDoc = parser.parseFromString(result, "text/xml");

               //It is necessery to reset the xsltProcessor befor doing another transfomation with it.
               xsltProcessor.reset();
               xsltProcessor.importStylesheet(amountXSL);
         			 var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
       				 document.getElementById("divConvertedAmount").innerHTML='';
       				 document.getElementById("divConvertedAmount").appendChild(resultDocument);
             }   
 	    }
      catch (e)
	    {
	        alert('Parser exception:'+e.description);
	    }
}

function curResultsAjax(result)
{
   var styler='';

   try
    	{
             if(browsertype=='IE')
             {
               xmlDoc.loadXML (result);
               styler= xmlDoc.transformNode(listToXSL.documentElement);
             }
             else
             {  
               xmlDoc = parser.parseFromString(result, "text/xml");

               //It is necessery to reset the xsltProcessor befor doing another transfomation with it.
               xsltProcessor.reset();
               xsltProcessor.importStylesheet(listToXSL);
         			 var resultDocument = xsltProcessor.transformToFragment(xmlDoc, document);
       				 document.getElementById("divCurrencyLists").innerHTML='';
       				 document.getElementById("divCurrencyLists").appendChild(resultDocument);
       				 styler=document.getElementById("divCurrencyLists").innerHTML;
             }   
 	    }
      catch (e)
	    {
	        alert('Parser exception:'+e.description);
	    }

    var dcl;
    dcl  = "<table class='tableentry'>";
    dcl +=  "<tr><td>Amount to convert from:</td><td><input type='text' name='amount' id='amount' value='1'></td></tr>";
    dcl += "<tr><td>Convert From:</td><td><select name='currencyFrom' id='currencyFrom'>";
    dcl += styler;
    dcl += "</select></td><td>Convert To:</td>";
    dcl += "<td><select name='currencyTo' id='currencyTo'>";
    dcl += styler;
    dcl += "</select></td></tr>";
    dcl += "<tr><td>Call service</td><td><input type='button' value='Convert!' onClick='convertRateAjax();'></td></tr></table>"
    document.getElementById("divCurrencyLists").innerHTML = dcl;
}


function dWait(dowhat){
  if(dowhat=='show'){
    document.getElementById('divWait').style.visibility='visible';
  }else{
    document.getElementById('divWait').style.visibility='hidden';
  }
}

</SCRIPT>
</HEAD>
<body onLoad="ajax.Start(currencyList,'')">
    <xml ID="amountXSL" SRC="converted_value_ajax.xsl"></xml>
    <xml ID="listToXSL" SRC="currency_list_ajax.xsl"></xml>
    <form method="POST" name="conversion">
      <DIV ID="divCurrencyLists"></DIV>
    </form>

      <script defer="defer" type="text/javascript">
          var currencyList = {
            delay: 200,
            prepare: function() {return;},
            call: proxies.SOAP.currencyList,
            finish: function(p){curResultsAjax(p);},
            onException: proxies.alertException
          }

          var convertRate = {
            delay: 200,
            prepare: function() {dWait('show');return convertRatePrepare();},
            call: proxies.SOAP.convertRate,
            finish: function(p){dWait('hide');amResultsAjax(p);},
            onException: proxies.alertException
          }

      </script>

    <DIV ID="divWait" style="visibility:hidden;"><font color="red">Please wait...</font></DIV>
    <hr>
    <DIV ID="divConvertedAmount">Enter an amount and choose the currencies you wish to convert from and to.</DIV>
</body>
</html>
